package com.javafactroy.kunlong.dataright;

import com.github.pagehelper.PageHelper;
import com.javafactroy.kunlong.DbAgent;
import com.javafactroy.kunlong.base.config.datasource.JdbcTemplateFactroy;
import com.javafactroy.kunlong.base.context.ApiContext;
import com.javafactroy.kunlong.data.entity.EmpInfoApiEntity;
import com.javafactroy.kunlong.domain.generalquery.agg.QueryMasterAgg;
import com.javafactroy.kunlong.domain.generalquery.repository.QueryMasterAggRepository;
import com.javafactroy.kunlong.domain.generalright.agg.UserRightAgg;
import com.javafactroy.kunlong.domain.generalright.repository.UserRightRepository;
import com.javafactroy.kunlong.common.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.util.ProxyUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import com.javafactroy.kunlong.data.dto.DrGroupApiDto;
import com.javafactroy.kunlong.data.service.DrGroupApiService;
import com.github.jsonzou.jmockdata.JMockData;
import com.github.jsonzou.jmockdata.MockConfig;
import org.junit.jupiter.api.*;

import java.util.List;
import java.util.Map;


/**
 * @author raymond.lei@163.com
 * @date 2023-11-30 10:54:35
 */

@ExtendWith(SpringExtension.class)
@Slf4j
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@TestMethodOrder(MethodOrderer.MethodName.class)
@SpringBootTest(classes = {DbAgent.class}, webEnvironment = SpringBootTest.WebEnvironment.NONE)
public class JdbcTemplateGenericTestApiService {

    @Autowired
    JdbcTemplateFactroy jdbcTemplateFactroy;

    @Autowired
    UserRightRepository userRightRepository;

    @Autowired
    DrGroupApiService service;

    String pkeyField = "id";
    String pkeyProp = "id";

    public DrGroupApiDto mock() {
        MockConfig mockConfig = new MockConfig()
                .doubleRange(1e-9d, 11112222.99999d)
                .floatRange(1e-8f, 11111112.99999f)
                .decimalScale(52).excludes(pkeyProp, "orderBys", "pageNum", "pageSize")
                .subConfig("status").intRange(0, 9).globalConfig();
        DrGroupApiDto entity = JMockData.mock(DrGroupApiDto.class, mockConfig);
        log.info("mock entity = {}", CommonUtils.toJSONString(entity));
        return entity;
    }

    @Autowired
    QueryMasterAggRepository queryMasterAggRepository;

    //@Autowired

    @BeforeEach
    void setUp() {
        log.info("setUp{}", "...");
    }

    @AfterEach
    void tearDown() {
        log.info("tearDown{}", "...");
    }

    @Test
    public void test001_selectsqlObject() {
        JdbcTemplate template = ApiContext.INST.getMysqlJdbcTemplate();
        //EnumQueryOprtType.values();
        QueryMasterAgg agg = queryMasterAggRepository.load(1L);
        log.info("select={}", agg.buildSelectSql().toString());
        PageHelper.startPage(1, 2);
        List<EmpInfoApiEntity> list = jdbcTemplateFactroy.findMysql().query("select * from emp_info where  emp_id=61",
                new BeanPropertyRowMapper(EmpInfoApiEntity.class));
        list = queryMasterAggRepository.query("select * from emp_info where emp_id=61", EmpInfoApiEntity.class);
        log.info("emp = {}", CommonUtils.toJSONString(list));
    }

    @Test
    public void test002_selectsqlMap() {
        // JdbcTemplate template = ApiContext.INST.getMysqlJdbcTemplate();

        QueryMasterAgg agg = queryMasterAggRepository.load(1L);
        agg.parseParams().putParam("@deptIdStart", 100).putParam("@deptIdEnd", 100);
        // @deptIdStart and @deptIdEnd
        PageHelper.startPage(1, 2);
        // List<Map<String, Object>> list = jdbcTemplateFactroy.findMysql().queryForList("select * from emp_info where  emp_id=61");
        // List<Map<String, Object>> list = jdbcTemplateFactroy.findMysql().queryForList(agg.buildSelectSql().toString());
        List<Map<String, Object>> list = queryMasterAggRepository.queryForList(agg.setPageSize(1));
        log.info("emp = {}", CommonUtils.toJSONString(list));
    }

    @Test
    public void test003_query() throws InstantiationException, IllegalAccessException {

        UserRightAgg userRightAgg = userRightRepository.load(1l).mergeRightEmps();
        log.info("load = {}", CommonUtils.toJSONString(userRightAgg));
        userRightAgg=null;
        Class<?> cls = ProxyUtils.getUserClass(userRightAgg);
        Object o = cls.newInstance();
        log.info("{}", o);
    }

    @Test
    public void test004_selectByPkey() {
        DrGroupApiDto entity = service.selectByPkey(1l);
        log.info("selectByPkey entity = {}", entity);

    }

    @Test
    public void test005_save() {

        DrGroupApiDto entity = mock();
        //service.save(entity.setShopName("122").setContact("ddl"));

        service.save(entity);
        log.info("save entity = {}", entity);

    }

    DrGroupApiDto save() {
        DrGroupApiDto entity = mock();
        service.save(entity);
        return entity;
    }

}

